package com.itheima.manager;

import com.itheima.domain.UmsMenu;
import com.itheima.domain.UmsRole;
import com.itheima.domain.UmsRoleMenuRelation;
import com.itheima.service.db.UmsMenuService;
import com.itheima.service.db.UmsRoleMenuRelationService;
import com.itheima.service.db.UmsRoleService;
import com.itheima.util.R;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName UmsRoleManager
 * @date 2022/11/12 14:42
 */
@Component
public class UmsRoleManager {

    @DubboReference
    UmsRoleService umsRoleService;

    @DubboReference
    UmsMenuService umsMenuService;

    @DubboReference
    UmsRoleMenuRelationService umsRoleMenuRelationService;

    // 获取所有角色
    public R listAll() {
        List<UmsRole> umsRoleList = umsRoleService.findByAllList();
        return R.success(umsRoleList);
    }

    // 获取指定用户的角色
    public R findByRoleList(Long adminId) {
        List<UmsRole> umsRoleList = umsRoleService.findByRoleList(adminId);
        return R.success(umsRoleList);
    }

    // 给用户分配角色
    public R allocationRole(Long adminId, List<Long> roleIds) {
        umsRoleService.allocationRole(adminId,roleIds);
        return R.success("操作成功");
    }

    // 根据角色名称分页获取角色列表
    public R getList(Integer pageNum, Integer pageSize, String keyword) {
        PageBeanVo list = umsRoleService.findByList(pageNum, pageSize, keyword);
        return R.success(list);
    }

    /**
     * 获取角色相关菜单
     * @param roleId
     * @return
     */
    public List<UmsMenu> getRoleListMenu(Long roleId) {
        // 根据角色id在角色菜单关系表中查询数据
        List<UmsRoleMenuRelation> roleListMenu = umsRoleMenuRelationService.findByRoleId(roleId);

        // 遍历集合获取菜单id,去菜单表中查取菜单,并装入集合
        List<UmsMenu> list = new ArrayList<>();
        roleListMenu.forEach(item -> {
            Long menuId = item.getMenuId();
            UmsMenu umsMenu = umsMenuService.findById(menuId);
            list.add(umsMenu);
        });
        return list;
    }

    /**
     * 给角色分配菜单
     * @param roleId
     * @param menuIds
     * @return
     */
    public R saveAllocMenu(Long roleId, List<Long> menuIds) {
         umsRoleMenuRelationService.saveAllocMenu(roleId,menuIds);
         return R.success("操作成功");
    }

    /**
     * 修改角色
     * @param id
     * @param umsRole
     * @return
     */
    public R roleUpdate(Long id, UmsRole umsRole) {
        umsRoleService.roleUpdate(id,umsRole);
        return R.success("操作成功");
    }

    /**
     * 批量删除角色
     * @param ids
     * @return
     */
    public R delete(List<Long> ids) {
        Integer delete = umsRoleService.delete(ids);
        if (delete == 1) {
            return R.success("删除成功");
        } else {
            return R.error(101,"账号状态启用中,删除失败");
        }
    }

    /**
     * 添加角色
     * @param umsRole
     * @return
     */
    public R create(UmsRole umsRole) {
        umsRoleService.create(umsRole);
        return R.success("添加成功");
    }

    /**
     * 给角色分配资源
     * @param resourceIds
     * @param roleId
     * @return
     */
    public R allocResource(List<Integer> resourceIds, Long roleId) {
        umsRoleService.allocResource(resourceIds,roleId);
        return R.success("添加完成");
    }

    /**
     * 修改角色状态
     * @param id
     * @param status
     * @return
     */
    public R updateStatus(Long id, Integer status) {
        umsRoleService.updateStatus(id,status);
        return R.success("修改成功");
    }
}
